package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.RechargeItem;
import cn.wolfcode.crm.domain.SaleOut;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.query.JSONResult;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.service.IRechargeItemService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("client")
public class ClientController {

    @Autowired
    private IClientService clientService;
    @Autowired
    private IRechargeItemService rechargeItemService;

    //由于dataGrid组件返回必须为json,所以跳转界面就分开
    @RequestMapping("view")
    public String view() {
        return "client";
    }

    //页面数据
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(ClientQueryObject qo) {
        return clientService.query(qo);
    }

    //编辑界面需要
    @RequestMapping("list")
    @ResponseBody
    public List<Client> list(){
        return clientService.selectAll();
    }

    //编辑
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Client client) {
        JSONResult result = new JSONResult();
        try {
            clientService.saveOrUpdate(client);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //删除
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            clientService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //查询当天生日人数
    @RequestMapping("bircount")
    @ResponseBody
    public Integer bircount() {
        try {
            return clientService.selectBirCount();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    //查询当月生日人数
    @RequestMapping("bimcount")
    @ResponseBody
    public Integer bimcount() throws ParseException {
        return clientService.selectbimcount();
    }
    //查询会员人数
    @RequestMapping("getCount")
    @ResponseBody
    public Integer getCount() throws ParseException {
        return clientService.getCount();
    }
    //表单用户名的校验
    @RequestMapping("validateByName")
    @ResponseBody
    public Boolean validateByName(String name, Long id) {
        return clientService.selectByName(name, id);
    }
    //表单卡号的校验
    @RequestMapping("validateByNumber")
    @ResponseBody
    public Boolean validateByNumber(String number, Long id) {
        return clientService.selectByNumber(number, id);
    }

    //累计消费
    @RequestMapping("getTotalExpense")
    @ResponseBody
    public BigDecimal getTotalExpense(){
        return rechargeItemService.getTotalExpense();
    }

    //累计比数
    @RequestMapping("getTotalCount")
    @ResponseBody
    public Integer getTotalCount(){
        return rechargeItemService.getTotalCount();
    }
    //前三
    @RequestMapping("getTOP")
    @ResponseBody
    public List<Map<String,Object>> getTOP(){
        return rechargeItemService.getTOP();
    }

    //购买时的密码认证
    @RequestMapping("loginBysale")
    @ResponseBody
    public JSONResult loginBysale(String name, String password) {
        JSONResult result = new JSONResult();
        try{
            clientService.loginBysale(name, password);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //导入
    @RequestMapping("importData")
    public String importData(MultipartFile file) throws IOException {
        clientService.importData(file);
        return "redirect:/client/view.do";
    }
    
    //导出
    @RequestMapping("exportData")
    public void exportData(HttpServletResponse response,ClientQueryObject qo) throws IOException {
        //设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=client.xls");
        //创建文件对象
        Workbook wb = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = wb.createSheet("clientData");
        //查询员工数据(要带高级查询的参数)
        List<Client> clientList = clientService.selectExportData(qo);
        //创建第一行
        Row row = sheet.createRow(0);
        //设置标题
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("真实姓名");
        row.createCell(2).setCellValue("电话");
        row.createCell(3).setCellValue("密码");
        //循环数据
        for (int i = 0; i < clientList.size(); i++) {
            //从第二行开始创建行
            Row row1 = sheet.createRow(i + 1);
            Client client = clientList.get(i);
            //创建单元格
            row1.createCell(0).setCellValue(client.getName());
            row1.createCell(1).setCellValue(client.getLevel());
            row1.createCell(2).setCellValue(client.getTel());
            row1.createCell(3).setCellValue(client.getPassword());
        }

        //输出到单元格
        wb.write(response.getOutputStream());
    }

    //查询单个会员信息
    @RequestMapping("get")
    @ResponseBody
    public Client get(Long id){

        return clientService.selectByPrimaryKey(id);
    }

}
